<?php
// +----------------------------------------------------------------------
// | 有理想的地方，地狱都是天堂。
// +----------------------------------------------------------------------
// | Copyright @ df81.com 版权所有
// +----------------------------------------------------------------------
// | 作者: 南国老符 邮箱：1062129401@qq.com
// +----------------------------------------------------------------------

namespace app\admin\model;
use think\Model;
use think\facade\Db;

class Sort extends Model{
	protected $auto = ['level'];

    // 级别：树形深度
    protected function setLevelAttr(){
    	$level = 0; $parentid = input('parentid');
    	if($parentid > 0){
    		$parentRs = Db::name('sort')->find($parentid);
    		$level = $parentRs['level'] + 1;
    	}
		return $level;
    }
    
	// 目录
    protected function setTabledirAttr(){
    	// 转为小写字母
        $general = new \common\General();
        $tabledir = $general->capital_to_underline(input('tabledir'));
		return $tabledir;
    }

    // urlroute
    protected function setUrlrouteAttr(){
		return strtolower(msubstr(input('urlroute'),0,100,''));
    }

    // description
    protected function setDescriptionAttr(){
        return msubstr(input('description'),0,500);
    }

    // 新增编辑
	static function edit($id){
	    if($id>0){// 编辑
			$sort = Sort::find($id);
		}else{// 新增
			$sort = new Sort;
		}
		$sort->save(input('post.'));
		return $sort->id;
	}

	/**列表
    * tabledir 模块目录
    * listNum 每页显示数
    * lang 语言
    * signid 状态
    * keyword 关键词
    * order 排序
    */
	static function index($tabledir,$listNum = 15,$lang,$signid = 1,$keyword,$order){
		// 转为小写字母
        $general = new \common\General();
        $tabledir = $general->capital_to_underline($tabledir);
        $keyword = trim($keyword);
        
        $where[] = ['lang','=',$lang];
		$where[] = ['tabledir','=',$tabledir];
		if($signid > 8){//已软删除
			$where[] = ['signid','>','8'];
		}else{
			$where[] = ['signid','=',$signid];
		}

		$pageParam = array('lang'=>$lang);
		$pageParam += array('tabledir'=>$tabledir);
		$pageParam += array('signid'=>$signid);
		$pageParam += array('keyword'=>$keyword);
		if(empty($keyword)){
			$where[] = ['parentid','=',0];
			$pageParam += array('parentid'=>0);
		}
		
		$rs = Db::name('sort')->where($where)->where('title|seotitle|keyword|description','like',"%$keyword%")->order($order)->paginate([
            	'list_rows'=> $listNum,
            	'query'=> $pageParam,
            ]);
		
		return $rs;
	}
}